//
//  ZHClinicStatisticsViewController.m
//  Doctor
//
//  Created by liuzhishan on 15/12/16.
//  Copyright © 2015年 shanshan. All rights reserved.
//

#import "ZHClinicStatisticsViewController.h"
#import "ZHClinicStatisticsCollectionView.h"
#import "ZHCircleView.h"
#import "ZHShowSpline.h"
#import "ZHClinicAllData.h"
@interface ZHClinicStatisticsViewController ()

@property (nonatomic,strong) ZHClinicStatisticsCollectionView * StatisticsView;
@property (nonatomic,strong) ZHCircleView *circleView;

@end

@implementation ZHClinicStatisticsViewController

-(ZHCircleView *)circleView
{
    if (_circleView == nil) {
        
        _circleView = [[ZHCircleView alloc]init];
        
        [_circleView setMyFramewithX:70 andY:180 andWidth:175 andHeight:175];

        [self.view addSubview:_circleView];
        
    }
    
    return _circleView;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.naviTitleLable.text = @"诊所统计";
    self.lableTitle.text = @"预约到店率";
    
    
    ZHClinicStatisticsCollectionView *staticView = [[ZHClinicStatisticsCollectionView alloc]init];
    
    [staticView setMyFramewithX:110 andY:375 andWidth:845 andHeight:286];

    [self.view addSubview:staticView];
    self.StatisticsView = staticView;
    
    ZHShowSpline *show0 = [[ZHShowSpline alloc]init];
    [show0  setMyFramewithX:380 andY:290 andWidth:85 andHeight:70];
    show0.imageViewUp.image = [UIImage imageNamed:@"green"];
    show0.lableDown.text = @"未预约到店";//所有患者
    [self.view addSubview:show0];
    
    
    ZHShowSpline *show1 = [[ZHShowSpline alloc]init];
    [show1  setMyFramewithX:550 andY:290 andWidth:85 andHeight:70];
    show1.imageViewUp.image = [UIImage imageNamed:@"orange"];
    show1.lableDown.text = @"预约到店";
    [self.view addSubview:show1];
    
    ZHShowSpline *show2 = [[ZHShowSpline alloc]init];
    [show2  setMyFramewithX:720 andY:290 andWidth:85 andHeight:70];
    show2.imageViewUp.image = [UIImage imageNamed:@"pink colour"];
    show2.lableDown.text = @"预约未到店";
    [self.view addSubview:show2];
    
    
    
    [self getDataWithAFN];
    
    
    __weak  ZHClinicStatisticsViewController  *weakSelf =self;
    [self.timePickerView setMyBlock:^(id responseObject) {
        
        
        NSMutableArray *array = [ZHClinicModelTool getDateArrayWithResponseObject:responseObject];
        
        [ZHSaveClickDate shareData];
        [ZHSaveClickDate getClassValueWithArray:array];
        
        weakSelf.StatisticsView.dataArray = array;
        
        [weakSelf reDrawMyCircle];
        
        
        
    }];

 
    
}


-(void)getDataWithAFN
{
    [AFNData getAFNDataWithUrl:self.clinicURL success:^(id responseObject) {
    
    
        NSMutableArray *array = [ZHClinicModelTool getDateArrayWithResponseObject:responseObject];
        
        [ZHSaveClickDate shareData];
        [ZHSaveClickDate getClassValueWithArray:array];
        
        self.StatisticsView.dataArray = array;
        
        
        [self reDrawMyCircle];

        
        
        //nslog(@"%@",responseObject);
        
         
        
    } failure:^(NSError *error) {
        
        
    }];
}


-(void)reDrawMyCircle
{
    
    if (_circleView ) {
        
        [_circleView removeFromSuperview];
        _circleView = nil;
    }
    
    CGFloat  all = 0;
    CGFloat  appointArrive  =  0;
    CGFloat  appointNotArrive =  0;
    

    
    
    for (ZHClinicAllData *model in self.StatisticsView.dataArray) {
        
        all += model.patienterNumber;
        appointArrive += model.appointmentNumber;
        appointNotArrive += model.notArrive;
        
    }
//    CGFloat all = arc4random() %40 + 40;
//    CGFloat appointArrive  =  arc4random() %10 + 10;
//    CGFloat  appointAll =  arc4random() %20 + 20;
    
    
    if (all == 0) {
        
    
        [self.circleView setInitialAngle:-M_PI_4 andFirstProportion:0 andSecondProportion:0 andThree:0];
        return;
        
    }
    
        CGFloat first =   (all - appointNotArrive - appointArrive)/all;
        CGFloat second =   appointArrive / all;

 
    [self.circleView setInitialAngle:-M_PI_4 andFirstProportion:first andSecondProportion:second andThree:(1.0-first-second)];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

 

@end
